package com.xxx.bluetooth;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xxx.bluetooth.bean.BluetoothInfo;

import java.util.LinkedList;

public class BluetoothInfoAdapter extends BaseAdapter {

    private LinkedList<BluetoothInfo> mListData;
    private Context mContext;

    public BluetoothInfoAdapter(LinkedList<BluetoothInfo> mListData, Context mContext) {
        this.mListData = mListData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_bluetooth_info, parent, false);
            holder = new ViewHolder();
            holder.tvName = (TextView)convertView.findViewById(R.id.name);
            holder.tvAddress = (TextView)convertView.findViewById(R.id.address);
            holder.tvRssi = (TextView)convertView.findViewById(R.id.rssi);
            holder.tvBondState = (TextView)convertView.findViewById(R.id.bondState);
            holder.tvDevType = (TextView)convertView.findViewById(R.id.deviceType);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.tvAddress.setText(mListData.get(position).getAddress());
        holder.tvName.setText(mListData.get(position).getName());
        holder.tvRssi.setText(""+mListData.get(position).getRssi());
        holder.tvDevType.setText(Util.deviceType2Str(mListData.get(position).getDeviceType()));
        holder.tvBondState.setText(Util.bondState2Str(mListData.get(position).getBondState()));

        return convertView;
    }


    private static class ViewHolder {
        TextView tvName;
        TextView tvAddress;
        TextView tvRssi;
        TextView tvDevType;
        TextView tvBondState;
    }
}
